package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.TBaseCost;
import com.ruoyi.system.mapper.TSpProjectDetailMapper;
import com.ruoyi.system.service.ITBaseCostService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TSpProjectDetail;
import com.ruoyi.system.service.ITSpProjectDetailService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目收入支出流水Controller
 * 
 * @author ruoyi
 * @date 2024-03-21
 */
@RestController
@RequestMapping("/system/detail")
public class TSpProjectDetailController extends BaseController
{
    @Autowired
    private ITSpProjectDetailService tSpProjectDetailService;

    @Autowired
    private TSpProjectDetailMapper tSpProjectDetailMapper;

    @Autowired
    private ITBaseCostService costService;

    /**
     * 查询项目收入支出流水列表
     */
    @PreAuthorize("@ss.hasPermi('system:detail:list')")
    @GetMapping("/list")
    public TableDataInfo list(TSpProjectDetail tSpProjectDetail)
    {
        startPage();
        List<TSpProjectDetail> list = tSpProjectDetailService.selectTSpProjectDetailList(tSpProjectDetail);
        return getDataTable(list);
    }

    /**
     * 导出项目收入支出流水列表
     */
    @PreAuthorize("@ss.hasPermi('system:detail:export')")
    @Log(title = "项目收入支出流水", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TSpProjectDetail tSpProjectDetail)
    {
        List<TSpProjectDetail> list = tSpProjectDetailService.selectTSpProjectDetailList(tSpProjectDetail);
        ExcelUtil<TSpProjectDetail> util = new ExcelUtil<TSpProjectDetail>(TSpProjectDetail.class);
        util.exportExcel(response, list, "项目收入支出流水数据");
    }

    /**
     * 获取项目收入支出流水详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:detail:query')")
    @GetMapping(value = "/{pdetailId}")
    public AjaxResult getInfo(@PathVariable("pdetailId") String pdetailId)
    {
        return success(tSpProjectDetailService.selectTSpProjectDetailByPdetailId(pdetailId));
    }

    /**
     * 新增项目收入支出流水
     */
    @PreAuthorize("@ss.hasPermi('system:detail:add')")
    @Log(title = "项目收入支出流水", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TSpProjectDetail tSpProjectDetail)
    {
        Long userId = SecurityUtils.getUserId();
        tSpProjectDetail.setCreatePerson(userId.toString());
        tSpProjectDetail.setCreateDate(DateUtils.getNowDate());
        tSpProjectDetail.setPdetailId(IdUtils.snowID());
        return toAjax(tSpProjectDetailService.insertTSpProjectDetail(tSpProjectDetail));
    }

    /**
     * 项目收入支出流水内部转账
     */
    @PreAuthorize("@ss.hasPermi('system:detail:add')")
    @Log(title = "项目收入支出流水内部转账", businessType = BusinessType.INSERT)
    @PostMapping("/addInOut")
    public AjaxResult addInOut(@RequestBody TSpProjectDetail tSpProjectDetail)
    {
        if(tSpProjectDetail.getProjectId() == null){
            //内部转账设置项目ID为0
            tSpProjectDetail.setProjectId(new Long(0));
        }
        Long userId = SecurityUtils.getUserId();
        tSpProjectDetail.setCreatePerson(userId.toString());
        tSpProjectDetail.setCreateDate(DateUtils.getNowDate());
        tSpProjectDetail.setPdetailId(IdUtils.snowID());

        //内部转入
        TBaseCost systemIn = costService.getOne(new LambdaQueryWrapper<TBaseCost>().eq(TBaseCost::getCostType, "1").eq(TBaseCost::getIsSystem, "1"));
        tSpProjectDetail.setPdetailId(IdUtils.snowID());
        tSpProjectDetail.setCostId(systemIn.getCostId());
        tSpProjectDetailService.insertTSpProjectDetail(tSpProjectDetail);

        //内部  转出
        TBaseCost systemOut = costService.getOne(new LambdaQueryWrapper<TBaseCost>().eq(TBaseCost::getCostType, "2").eq(TBaseCost::getIsSystem, "1"));
        TSpProjectDetail tSpProjectDetailOut = new TSpProjectDetail();
        tSpProjectDetailOut.setPdetailId(IdUtils.snowID());
        tSpProjectDetailOut.setProjectId(tSpProjectDetail.getProjectId());
        tSpProjectDetailOut.setBankId(tSpProjectDetail.getOutBankId()); //银行填转出银行ID
        tSpProjectDetailOut.setCostId(systemOut.getCostId());
        tSpProjectDetailOut.setDescription(tSpProjectDetail.getDescription());
        tSpProjectDetailOut.setVoucherNum(tSpProjectDetail.getVoucherNum());
        tSpProjectDetailOut.setPayCompany(tSpProjectDetail.getPayCompany());
        tSpProjectDetailOut.setCostDate(tSpProjectDetail.getCostDate());
        tSpProjectDetailOut.setCostMoney(tSpProjectDetail.getCostMoney());
        tSpProjectDetailOut.setRemark(tSpProjectDetail.getRemark());
        tSpProjectDetailOut.setCreatePerson(userId.toString());
        tSpProjectDetailOut.setCreateDate(DateUtils.getNowDate());
        return toAjax(tSpProjectDetailService.insertTSpProjectDetail(tSpProjectDetailOut));

    }

    /**
     * 修改项目收入支出流水
     */
    @PreAuthorize("@ss.hasPermi('system:detail:edit')")
    @Log(title = "项目收入支出流水", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TSpProjectDetail tSpProjectDetail)
    {
        Long userId = SecurityUtils.getUserId();
        tSpProjectDetail.setUpdatePerson(userId.toString());
        tSpProjectDetail.setUpdateDate(DateUtils.getNowDate());
        return toAjax(tSpProjectDetailService.updateTSpProjectDetail(tSpProjectDetail));
    }

    /**
     * 删除项目收入支出流水
     */
    @PreAuthorize("@ss.hasPermi('system:detail:remove')")
    @Log(title = "项目收入支出流水", businessType = BusinessType.DELETE)
	@DeleteMapping("/{pdetailIds}")
    public AjaxResult remove(@PathVariable String pdetailIds)
    {
        return toAjax(tSpProjectDetailService.deleteTSpProjectDetailByPdetailId(pdetailIds));
    }
}
